package com.zkr.subclass.conterllor;


import com.zkr.subclass.enummodel.StatusEnum;
import com.zkr.subclass.model.*;
import com.zkr.subclass.service.SubClassService;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2020/12/10.
 */
@RestController
@CrossOrigin
public class SubClassConterllor {

    @Autowired
    private SubClassService subClassService;



    //分页查询
    @PostMapping("subClass/queryPage")
    public ResultVo queryPage(@RequestBody SubClassSelectVo subClassSelectVo){


        return subClassService.queryPage(subClassSelectVo);
    }


    //添加
    @PostMapping("subClass/add")
    public ResultVo add(@RequestBody @Valid SubClassAddVo subClassAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return subClassService.add(subClassAddVo);
    }

    //根据id查看
    @GetMapping("queryById")
    public ResultVo queryById(Integer id){
        if (id== null){
            return ResultBuilderVo.error("查看时ID不能为空!");

        }
        return  subClassService.queryById(id);

    }

    //修改
    @PostMapping("subClass/update")
    public ResultVo update(@RequestBody @Valid SubClassUpdateVo subClassUpdateVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return subClassService.update(subClassUpdateVo);
    }

    //修改启用禁用状态
    @PostMapping("subClass/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid SubClassUpdateStatusVo subClassUpdateStatusVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return subClassService.updateStatus(subClassUpdateStatusVo);
    }


    /**
     * 下拉框
     *
     */
    @PostMapping("metadata/getPullDownList")
    public ResultVo xiala(){
        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (StatusEnum c:StatusEnum.values()){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setLabel(c.getName());
            xiaLaVo.setValue(c.getValue());
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultBuilderVo.success(xiaLaVoList);
    }


}
